package collection_;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author DCSGO
 * @version 1.0
 * 创建3个Dog{name,age}对象
 * 放入到ArrayList中，并赋给List引用
 * 用迭代器和foreach两种方式来遍历
 * 重写Dog的toString方法，输出name和age
 */
public class CollectionExercise {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Dog("like", 13));
        list.add(new Dog("live", 3));
        list.add(new Dog("luck", 5));

        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        //collection_.Dog@1b6d3586
        //collection_.Dog@4554617c
        //collection_.Dog@74a14482

        System.out.println("_________");
        for (Object obj : list) {
            System.out.println(obj);
        }
        //collection_.Dog@1b6d3586
        //collection_.Dog@4554617c
        //collection_.Dog@74a14482

        //重写 toString 后
        /*
         * name='like', age=13
         * name='live', age=3
         * name='luck', age=5
         * _________
         * name='like', age=13
         * name='live', age=3
         * name='luck', age=5
         */
    }
}

class Dog {
    String name;
    int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "name='" + name + '\'' +
                ", age=" + age;
    }
}
